package com.t.bricks.bussiness.db.dao.tidings.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.tidings.TidingsDao;
import com.t.bricks.bussiness.db.entity.tidings.Tidings;
import com.t.bricks.bussiness.db.mapper.tidings.TidingsMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class TidingsDaoImpl implements TidingsDao {
	
	@Autowired
	private TidingsMapper tidingsMapper;

	/**
	 * 添加
	 * @param tidings
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity add(Tidings tidings) {
		try {
			int i = tidingsMapper.add(tidings);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", tidings);
	}

	/**
	 * 搜索关键字对应的记录
	 * @param likeStr
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity search(String likeStr, String sUserId) {
		List<Tidings> list = null;
		try {
			list = tidingsMapper.search(likeStr, sUserId);
			if (null == list) {
				Log.error("搜索数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "搜索操作失败！", 7002);
			}
			
			return new MsgEmity(true, "查询成功！", list);
		} catch (Exception e) {
			Log.error("搜索数据发生异常:", e.getMessage());
			return new MsgEmity(false, "搜索请求失败，请查看异常日志！", 7003);
		}
	}
	
	/**
	 * 消息标记'删除'
	 * @param iId 记录编号
	 * @param isSsendee 是否接收者(不是接收者就是发送者)
	 * @param sUserId 操作用户
	 * @return
	 */
	@Override
	public MsgEmity delSign(Long iId, boolean isSsendee, String sUserId) {
		try {
			int i = tidingsMapper.delSign(iId, isSsendee, sUserId);
			if (i < 1) {
				Log.error("标记'删除'未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "标记'删除'数据失败！", 7001);
			}
			
			tidingsMapper.clear();//清理双方都删除的数据,不需要事务
		} catch (Exception e) {
			Log.error("标记'删除'时发生异常:", e.getMessage());
			return new MsgEmity(false, "标记'删除'失败！", 7002);
		}
		
		return new MsgEmity(true, "标记'删除'成功！", 7999);
	}
	
	/**
	 * 查询指定用户指定时间的对话信息
	 * @param stDate 开始时间
	 * @param edDate 结束时间
	 * @param sUserId 用户编号(对应接收者或发送者)
	 * @return List<Tidings>
	 */
	@Override
	public MsgEmity findByDate(Date stDate, Date edDate, String sUserId) {
		List<Tidings> list = null;
		try {
			list = tidingsMapper.findByUserAndDate(stDate, edDate, sUserId);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
			
			return new MsgEmity(true, "查询成功！", list);
		} catch (Exception e) {
			Log.error("查询数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
	}

}
